﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using SubSonic.Query;
using SubSonic.Schema;

namespace PCS.Core
{
    public class NewsController
    {
        public static PagedList<News> GetList(int pageIndex, int pageSize, OrderDirection orderDirection, out int totalRecords)
        {
            IQueryable<News> list;
            if (OrderDirection.ASC == orderDirection)
            {
                list = News.All().OrderBy(i => i.Date);
            }
            else
            {
                list = News.All().OrderByDescending(i => i.Date);
            }
            totalRecords = list.Count<News>();

            return list.ToPagedList(pageIndex-1, pageSize);
        }

        public static PagedList<News> GetListByType(string type, int pageIndex, int pageSize, OrderDirection orderDirection, out int totalRecords)
        {
            IQueryable<News> list;
            if (OrderDirection.ASC == orderDirection)
            {
                list = News.All().OrderBy(i => i.Date).Where(i => i.Type == type);
            }
            else
            {
                list = News.All().OrderByDescending(i => i.Date).Where(i => i.Type == type);
            }
            totalRecords = list.Count<News>();

            return list.ToPagedList(pageIndex-1, pageSize);
        }
    }
}
